<template>
    <div class="background-container">
        <div class="container">
            <div class="login-form">
                <h3 class="title">注册账号</h3>
                <el-form class="rgs-form">
                    <el-form-item class="input">
                        <span class="svg-container"><svg-icon icon-class="user" /></span>
                        <el-input v-model="ruleForm.username" autocomplete="off" placeholder="请输入账号" />
                    </el-form-item>
                    <el-form-item class="input">
                        <span class="svg-container"><svg-icon icon-class="password" /></span>
                        <el-input type="password" placeholder="请输入密码" v-model="ruleForm.password" autocomplete="off"
                            show-password />
                    </el-form-item>
                    <el-form-item class="input">
                        <span class="svg-container"><svg-icon icon-class="password" /></span>
                        <el-input type="password" placeholder="请确认密码" v-model="ruleForm.repetitionPassword"
                            autocomplete="off" show-password />
                    </el-form-item>
                    <el-form-item class="input" v-if="tableName=='yonghu'">
                        <span class="svg-container"><svg-icon icon-class="peoples" /></span>
                        <el-input v-model="ruleForm.yonghuName" autocomplete="off" placeholder="请输入姓名" />
                    </el-form-item>
                    <el-form-item class="input" v-if="tableName=='yonghu'">
                        <span class="svg-container"><svg-icon icon-class="phone" /></span>
                        <el-input v-model="ruleForm.yonghuPhone" autocomplete="off" placeholder="请输入电话" />
                    </el-form-item>
                    <el-form-item class="input" v-if="tableName=='yonghu'">
                        <span class="svg-container"><svg-icon icon-class="validCode" /></span>
                        <el-input v-model="ruleForm.yonghuIdNumber" autocomplete="off" placeholder="请输入身份证" />
                    </el-form-item>
                    <div class="button-container">
                        <el-button class="btn" type="primary" @click="login()">注册</el-button>
                        <el-button class="btn close" type="primary" @click="close()">取消</el-button>
                    </div>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                },
                tableName:"",
                rules: {},
                sexTypesOptions : [],
            };
        },
        mounted(){
            let table = this.$storage.get("loginTable");
            this.tableName = table;

            //级联表的下拉框查询

        },
        methods: {
            // 获取uuid
            getUUID () {
                return new Date().getTime();
            },
            // 验证身份证号码
            validateIdCard(idCard) {
                // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
                const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                if (!reg.test(idCard)) {
                    return false;
                }
                
                // 如果是15位身份证
                if (idCard.length === 15) {
                    return true;
                }
                
                // 18位身份证需要验证最后一位校验位
                const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                const parity = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
                let sum = 0;
                let ai = 0;
                let wi = 0;
                
                for (let i = 0; i < 17; i++) {
                    ai = idCard[i];
                    wi = factor[i];
                    sum += ai * wi;
                }
                
                const last = parity[sum % 11];
                return last === idCard[17].toUpperCase();
            },
            close(){
                this.$router.push({ path: "/login" });
            },
            // 注册
            login() {

                            if((!this.ruleForm.username)){
                                this.$message.error('账号不能为空');
                                return
                            }
                            if((!this.ruleForm.password)){
                                this.$message.error('密码不能为空');
                                return
                            }
                            if((!this.ruleForm.repetitionPassword)){
                                this.$message.error('重复密码不能为空');
                                return
                            }
                            if(this.ruleForm.repetitionPassword != this.ruleForm.password){
                                this.$message.error('密码和重复密码不一致');
                                return
                            }
                            if((!this.ruleForm.yonghuName)&& 'yonghu'==this.tableName){
                                this.$message.error('用户姓名不能为空');
                                return
                            }
                             if('yonghu' == this.tableName && this.ruleForm.yonghuPhone&&(!this.$validate.isMobile(this.ruleForm.yonghuPhone))){
                                 this.$message.error('手机应输入手机格式');
                                 return
                             }
                            if((!this.ruleForm.yonghuIdNumber)&& 'yonghu'==this.tableName){
                                this.$message.error('用户身份证号不能为空');
                                return
                            }
                            if('yonghu' == this.tableName && this.ruleForm.yonghuIdNumber){
                                if(!this.validateIdCard(this.ruleForm.yonghuIdNumber)){
                                    this.$message.error('请输入正确的身份证号码格式');
                                    return
                                }
                            }
                this.$http({
                    url: `${this.tableName}/register`,
                    method: "post",
                    data:this.ruleForm
                }).then(({ data }) => {
                    if (data && data.code === 0) {
                    this.$message({
                        message: "注册成功,请登录后在个人中心页面补充个人数据",
                        type: "success",
                        duration: 1500,
                        onClose: () => {
                        this.$router.replace({ path: "/login" });
                }
                });
                } else {
                    this.$message.error(data.msg);
                }
            });
            }
        }
    };
</script>

<style lang="scss" scoped>
    .background-container {
        background-color: #def0ff;
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    body {
        padding: 0;
        margin: 0;
        font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
    }

    .container {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 20px;
    }

    .login-form {
        position: relative;
        border-radius: 8px;
        background-color: #ffffff;
        width: 480px;
        padding: 40px;
        box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
        transition: all 0.3s ease;
    }

    .title {
        color: rgba(60, 60, 60, 1);
        margin: 20px 0;
        text-align: center;
        font-size: 24px;
    }

    .rgs-form {
        width: 80%;
        margin: 0 auto;
    }

    .button-container {
        display: flex;
        flex-wrap: wrap;
        width: 100%;
        justify-content: center;
        margin-top: 20px;
    }

    .input {
        width: 100%;
        margin: 10px auto;
        position: relative;

        .svg-container {
            scale: 1.2;
            padding: 0;
            color: #889aa4;
            position: absolute;
            left: -20px;
            top: 60%;
            transform: translateY(-50%);
            z-index: 2;
        }

        .el-input {
            margin-left: 5%;
            width: 95%;

            input {
                border: 1px solid #dcdfe6;
                border-radius: 4px;
                background: transparent;
                padding-left: 45px !important;
                height: 44px;
                line-height: 44px;

                &:focus {
                    border-color: #409EFF;
                }
            }
        }
    }

    /deep/ .svg-icon {
        width: 18px;
        height: 18px;
    }

    .btn {
        margin: 0 10px;
        width: 120px;
        height: 44px;
        line-height: 44px;
        color: #fff;
        font-size: 16px;
        font-weight: 500;
        border: none;
        border-radius: 4px;
        background-color: #408aff;
        transition: all 0.3s ease;
        cursor: pointer;
        text-align: center;
        padding: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        
        &:hover {
            transform: translateY(-2px);
            box-shadow: 0 7px 14px rgba(50, 50, 93, 0.1), 0 3px 6px rgba(0, 0, 0, 0.08);
            opacity: 0.9;
        }
    }

    .close {
        margin: 0 10px;
        width: 120px;
        height: 44px;
        line-height: 44px;
        color: #408aff;
        font-size: 16px;
        font-weight: 500;
        border: 1px solid #408aff;
        border-radius: 4px;
        background-color: transparent;
        transition: all 0.3s ease;
        
        &:hover {
            background-color: rgba(64, 138, 255, 0.1);
            transform: translateY(-2px);
        }
    }
    
    @media (max-width: 576px) {
        .login-form {
            width: 100%;
            padding: 30px 20px;
        }
        
        .btn, .close {
            width: 100%;
            margin: 5px 0;
        }
        
        .button-container {
            flex-direction: column;
        }
    }
</style>
